package com.example.mall.query;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Data
@ApiModel(description = "分页查询结构")
public class PageDTO<T>{
    @ApiModelProperty(value = "总条数")
    private Long total;
    @ApiModelProperty(value = "总页数")
    private Long pages;
    @ApiModelProperty(value = "数据集合")
    private List<T> list;

    /**
     * 不需要做特殊处理的拷贝
     * @param p
     * @param clazz
     * @return
     * @param <PO>
     * @param <VO>
     */
    public static <PO,VO> PageDTO<VO> of(Page<PO> p, Class<VO> clazz){
        PageDTO<VO> dto = new PageDTO<>();
        //1.总条数
        dto.setTotal(p.getTotal());
        //2.总页数
        dto.setPages(p.getPages());
        //3.当前页数据
        List<PO> records = p.getRecords();
        if (CollectionUtils.isEmpty(records)){
            dto.setList(Collections.emptyList());
            return dto;
        }
        //4.拷贝到vo
        dto.setList(BeanUtil.copyToList(records,clazz));
        //5.返回
        return dto;
    }

    /**
     * 需要特殊手动处理的拷贝
     * @param p
     * @param convertor
     * @return
     * @param <PO>
     * @param <VO>
     */
    public static <PO,VO> PageDTO<VO> of(Page<PO> p, Function<PO,VO> convertor){
        PageDTO<VO> dto = new PageDTO<>();
        //1.总条数
        dto.setTotal(p.getTotal());
        //2.总页数
        dto.setPages(p.getPages());
        //3.当前页数据
        List<PO> records = p.getRecords();
        if (CollUtil.isEmpty(records)){
            dto.setList(Collections.emptyList());
            return dto;
        }
        //4.拷贝到vo
        dto.setList(records.stream().map(convertor).collect(Collectors.toList()));
        //5.返回
        return dto;
    }
}
